-- region GScheduler.lua
-- Author : easeliu
-- Date   : 2015/06/09
-- Description ：全局调度器

------------------------------------------------------------------------------
GScheduler = class("GScheduler",function()
        local obj = {
            callback_list = {},
            frame_timer_list = {},
            net_timer = nil,
        }
        return obj
    end)
GScheduler.__index = GScheduler

-- 单例设计
GScheduler.__instance = nil

-- 创建调度器
function GScheduler:create() 
    local obj = GScheduler:new() 
    obj:init()
    GScheduler.__instance = obj
end

-- 初始化函数
function GScheduler:init()
    local onFrame = function()
        self:onFrame()
    end
    cc.Director:getInstance():getScheduler():scheduleScriptFunc(onFrame,0,false)
end

-- 获取单例
function GScheduler:ins()
    if  nil == GScheduler.__instance then
            GScheduler:create()
    end
    return GScheduler.__instance
end

-- 每帧执行
function GScheduler:onFrame()
    self:checkRunCallbackList()

    if  nil ~= self.net_timer  then
            self:net_timer()
    end
end

-- 检查执行回调列表
function GScheduler:checkRunCallbackList()
    local cbl = self.callback_list
    self.callback_list = {}
    for i,callback in pairs(cbl) do 
        cbl[i] = nil
        callback()
    --        log.warning("GScheduler","checkRunCallbackList: %d",#cbl)                
    end
end

-- 设置网络数据定时器
function GScheduler:setNetTimer(netTimer)
    self.net_timer = netTimer
end

-- 清除网络数据定时器
function GScheduler:clearNetTimer()
    self.net_timer = nil
end

-- 下一帧执行 (传入回调函数)
function GScheduler:doNextFrame(callback)
    table.insert(self.callback_list,callback)
end   

------------------------------------------------------------------------------
-- 下一帧执行函数
function doNextFrame(funcDoSomething) 
    GScheduler:ins():doNextFrame(funcDoSomething)
end

------------------------------------------------------------------------------

